package com.base.consumer;

import com.alibaba.fastjson.JSONObject;
import com.base.entity.DispatchEntity;
import com.base.entity.OrderEntity;
import com.base.repository.DispatchRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RocketMQMessageListener(topic = "orderTopic", consumerGroup = "myConsumer1")
public class OrderConsumer implements RocketMQListener<String> {
    @Autowired
    private DispatchRepository dispatchRepository;

    @Override
    public void onMessage(String msg) {
        OrderEntity orderEntity = JSONObject.parseObject(msg, OrderEntity.class);
        String orderId = orderEntity.getOrderId();
        // 模拟userid为=123456
        DispatchEntity dispatchEntity = new DispatchEntity(orderId, 123456L);
        dispatchRepository.save(dispatchEntity);
        log.info("myConsumer1: 消费一条消息，" + dispatchEntity);
    }

}
